import 'package:base/main.dart';
import 'package:flutter/material.dart';
import 'package:framework/config/constants.dart';
import 'package:module_discover/enums/domain_type_enum.dart';
import 'package:module_discover/pages/filter_domain/filter_domain_logic.dart';
import 'package:module_discover/pages/filter_domain/widgets/child/child_input2_widget.dart';
import 'package:module_discover/pages/filter_domain/widgets/child/child_single_widget.dart';
import 'package:module_discover/pages/model/filter_model.dart';

/// 自定义布局
class ChildCustomWidget extends StatelessWidget {
  final BaseFilterItem child;

  final FilterDomainLogic logic = Get.find<FilterDomainLogic>();

  ChildCustomWidget({
    super.key,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return _buildOptionView(child.custom);
  }

  Widget _buildOptionView(BaseFilterItem? custom) {
    if (custom == null) {
      return SizedBox();
    }
    final type = DomainTypeEnum.fromValue(custom.type);
    switch (type) {
      case DomainTypeEnum.INPUT2:
        if (custom.value != null && custom.value != "") {
          try {
            List<String> parts = custom.value!.split('-');
            logic.selectInputVM.getController("${custom.key}_1").text =
                parts[0];
            logic.selectInputVM.getController("${custom.key}_2").text =
                parts[1];
          } catch (e) {}
        }

        return ChildInput2Widget(
          name: custom.title,
          logic: logic,
          fieldKey1: "${custom.key}_1",
          onChanged1: (v) {
            _onChanged(custom);
          },
          fieldKey2: "${custom.key}_2",
          onChanged2: (v) {
            _onChanged(custom);
          },
        );
      case DomainTypeEnum.M_CHOICE:
        return _buildChoiceItemView(custom);
      case DomainTypeEnum.S_CHOICE:
        return _buildChoiceItemView(custom);
      default:
        Log.d('ChildCustomWidget _buildOptionView type = $type');
    }
    return SizedBox();
  }

  _onChanged(BaseFilterItem custom) {
    String parentKey = logic.domainTitles[logic.selectedIndex.value].value.key;
    final input1 = logic.selectInputVM.getController("${custom.key}_1").text;
    final input2 = logic.selectInputVM.getController("${custom.key}_2").text;
    if (input1.isNotEmpty && input2.isNotEmpty) {
      // 删除数据
      logic.deleteSelectedMap(parentKey, child.key);

      logic.addSelectMaps(
          childKey: child.key,
          sChoice: true,
          isSelected: true,
          name: child.title,
          key: custom.title,
          value: "$input1-$input2");
      for (var item in child.children) {
        item.isSelected.value = false;
      }
    }
  }

  _buildChoiceItemView(BaseFilterItem item) {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SizedBox(height: Constants.spaceBase),
          Text(
            item.title,
            style: TextStyle(
              fontSize: 13,
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
          ),
          ChildSingleWidget(child: item),
          SizedBox(height: 16),
        ],
      ),
    );
  }
}
